package br.edu.oxente.persistence.jpa;

import java.util.ResourceBundle;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Nigini Abilio - nigini@gmail.com
 */
public class JPAUtility {

	private static final String FILE_BASE = "project";
	private static final String USED_PU = "pu";
	private static EntityManagerFactory entityManagerFactory;
	private static EntityManager entityManager;
	private static ResourceBundle resourceBundle;
	private static String persistence;

	static {
		resourceBundle = ResourceBundle.getBundle(FILE_BASE);
		persistence = resourceBundle.getString(USED_PU);
	}

	/**
	 * Metodo que cria um EntityManager.
	 */
	private static void createEntityManager() {
		if ( entityManagerFactory == null ) {
			entityManagerFactory = Persistence.createEntityManagerFactory(persistence);
		}
		entityManager = entityManagerFactory.createEntityManager();
	}

	public static EntityManager getEntityManager() {
		createEntityManager();
		return entityManager;
	}
}
